package com.nari.customer.dao;

import java.util.ArrayList;
import java.util.List;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.customer.bo.UserComplaint;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;

public class CustomerComplaintsDao extends
		GenericDaoImpl<UserComplaint, String> implements ICustomerComplaintsDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ResultWithPageCond<UserComplaint> queryForPage(String buildingId,
			String projectId, String communityId, PageCond pageCond) {
		List<UserComplaint> lsRe = new ArrayList<UserComplaint>();
		try {
			String sql = " from UserComplaint where 1 = 1 ";
			List lst = new ArrayList();
			// 楼栋名称
			if (buildingId != null && !"".equals(buildingId.trim())) {
				sql += " and buildingId = ?";
				lst.add(buildingId.trim());
			}
			// 所属项目
			if (projectId != null && !"".equals(projectId.trim())) {
				sql += " and projectId = ?";
				lst.add(projectId.trim());
			}
			// 所属小区
			if (communityId != null && !"".equals(communityId.trim())) {
				sql += " and communityId = ?";
				lst.add(communityId.trim());
			}
			List<UserComplaint> ls = this.getHibernateTemplate().find(sql,
					lst.toArray());
			// 统计结果集个数
			int total = ls.size();
			// 结果集分页
			int limit = pageCond.getLimit() > ls.size() ? ls.size() : pageCond
					.getLimit();
			for (int i = pageCond.getStart(); i < limit; i++) {
				lsRe.add(ls.get(i));
			}
			// 构造返回类型
			pageCond.setTotal(total);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ResultWithPageCond<UserComplaint> page = new ResultWithPageCond<UserComplaint>();
		page.setList(lsRe);
		page.setPageCond(pageCond);
		return page;
	}

	public void updateObject(UserComplaint userComplaint) {
		getHibernateTemplate().update(userComplaint);
	}
}
